package fr.upstlse.glre.client.login;

import net.mygwt.ui.client.Events;
import net.mygwt.ui.client.Style;
import net.mygwt.ui.client.event.BaseEvent;
import net.mygwt.ui.client.event.Listener;
import net.mygwt.ui.client.widget.Dialog;
import net.mygwt.ui.client.widget.Info;
import net.mygwt.ui.client.widget.WidgetContainer;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.controler.ControlerService;
import fr.upstlse.glre.client.controler.ControlerServiceAsync;
import fr.upstlse.glre.client.espaceperso.EspacePerso;
import fr.upstlse.glre.client.vo.EtudiantVOGWT;
import fr.upstlse.glre.client.widgets.ErrorMessageBox;
import fr.upstlse.glre.client.widgets.FailureMessageBox;
import fr.upstlse.glre.client.widgets.InfoMessageBox;
import fr.upstlse.glre.client.widgets.TextField;

public class Login {

	ControlerServiceAsync controlerService;

	public Login() {
		controlerService = (ControlerServiceAsync) GWT
				.create(ControlerService.class);
		ServiceDefTarget lEndPoint = (ServiceDefTarget) controlerService;
		String lServiceUrl = GWT.getModuleBaseURL() + "controlerService";
		lEndPoint.setServiceEntryPoint(lServiceUrl);

		final Dialog dialog = new Dialog(Style.OK | Style.CLOSE);
		dialog.setCloseOnButtonClick(true);
		dialog.setText(RfgConstante.LOGIN_PANEL_TITLE);

		WidgetContainer lContent = dialog.getContent();

		VerticalPanel lMainPanel = new VerticalPanel();

		HorizontalPanel lFormContentPanel = new HorizontalPanel();
		final TextField lIdentifiantTextField = new TextField(
				RfgConstante.LOGIN_FIELD_IDENTIFIANT_LABEL,
				RfgConstante.LOGIN_FIELD_IDENTIFIANT_NAME);
		lFormContentPanel.add(lIdentifiantTextField);

		final TextField lMotDePasseTextField = new TextField(
				RfgConstante.LOGIN_FIELD_MOTDEPASSE_LABEL,
				RfgConstante.LOGIN_FIELD_MOTDEPASSE_NAME);
		lFormContentPanel.add(lMotDePasseTextField);

		lMainPanel.add(lFormContentPanel);

		lContent.add(lMainPanel);

		dialog.addListener(Events.Close, new Listener() {

			public void handleEvent(BaseEvent be) {
				final InfoMessageBox lInfoMessageBox = new InfoMessageBox("Chargement en cours...");
				lInfoMessageBox.open();
				AsyncCallback lCallback = new AsyncCallback() {

					public void onFailure(Throwable pCaught) {
						lInfoMessageBox.close();
						FailureMessageBox lFailureMessageBox = new FailureMessageBox("Erreur getEtudiant", pCaught.getMessage());
						lFailureMessageBox.open();
					}

					public void onSuccess(Object pResult) {
						lInfoMessageBox.close();
						Info.show("Information", "Succès du chargement", "");
						EtudiantVOGWT lEtudiant = (EtudiantVOGWT) pResult;
						if (lEtudiant.getId() == 0) {
							ErrorMessageBox lErrorMessageBox = new ErrorMessageBox("L'étudiant n'existe pas");
							lErrorMessageBox.open();
						} else {
							RootPanel.get().clear();
							EspacePerso lEspacePerso = new EspacePerso();
							lEspacePerso.createWidget(lEtudiant);
							lEspacePerso.initWidget();
						}
					}
				};

				controlerService.findEtudiant(lIdentifiantTextField.getValue(), lMotDePasseTextField.getValue(),
						lCallback);
			}

		});

		dialog.open();
	}
}
